package com.xunqi.product.service.impl;

import com.xunqi.product.entity.SpuInfoEntity;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xunqi.common.utils.PageUtils;
import com.xunqi.common.utils.Query;

import com.xunqi.product.dao.SkuInfoDao;
import com.xunqi.product.entity.SkuInfoEntity;
import com.xunqi.product.service.SkuInfoService;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<SkuInfoEntity> wrapper = new QueryWrapper<>();

        String key = params.get("key")==null?"":params.get("key").toString();
        if (!StringUtils.isEmpty(key)){
            wrapper.and(i->i.eq("sku_id",key).or().like("sku_name",key));
        }
        String brandId = params.get("brandId")==null?"":params.get("brandId").toString();
        if (!StringUtils.isEmpty(brandId)&&!brandId.equalsIgnoreCase("0")){
            wrapper.eq("brand_id",brandId);
        }
        String catelogId = params.get("catelogId")==null?"":params.get("catelogId").toString();
        if (!StringUtils.isEmpty(catelogId)&&!catelogId.equalsIgnoreCase("0")){
            wrapper.eq("catalog_id",catelogId);
        }
        String min = params.get("min")==null?"":params.get("min").toString();
        if (!StringUtils.isEmpty(min)&&!min.equalsIgnoreCase("0")){
            wrapper.ge("price",min);
        }
        String max = params.get("max")==null?"":params.get("max").toString();
        if (!StringUtils.isEmpty(min)&&!max.equalsIgnoreCase("0")){
            wrapper.le("price",max);
        }

        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public void saveSkuInfo(SkuInfoEntity skuInfoEntity) {
        this.save(skuInfoEntity);
    }

}